var Web3 = require("web3");
var fs = require('fs');
var os = require('os');
var config = require('./config/config.js')
var web3 = new Web3(new Web3.providers.HttpProvider(config.HttpProvider));


async function main(data, time){
    console.log('开始')
    var i = 0
    var start_time = (new Date()).getTime();
    while(true){
        i ++
        var addinfo = await web3.eth.accounts.create();
        if(check(data, addinfo.address)){
            var info = {
                address: addinfo.address,
                privateKey: addinfo.privateKey
            }
            insertStr = JSON.stringify(info) + os.EOL
            fs.appendFileSync('./files/address.txt', insertStr)
            console.log('写入记录')
        }
        var end_time = (new Date()).getTime();
        if(end_time - start_time >= time * 1000){
            console.log('计算数量：' + i)
            break;
        }
    }
}

function check(data, address){
    for (const value of data) {
        rega = eval("/^"+ value.start +"/")
        resa = rega.test(address)

        regb = eval("/"+ value.end +"$/")
        resb = regb.test(address)

        if(resa && resb){
            return true
        }
    }
    return false
}

var data = [
    {
        start:'0x0', 
        end:'8888'
    }
]
var time = 864000

main(data, time)

